//
//  StatusPictureView.m
//  OCMicroBlog
//
//  Created by 002 on 15/11/7.
//  Copyright © 2015年 002. All rights reserved.
//

#import "StatusCellPictureView.h"
#import "StatusCellPictureViewCell.h"
#import "SDWebImageManager.h"

@interface StatusCellPictureView ()<UICollectionViewDataSource>

// collectionView的 layout
@property(nonatomic,strong) UICollectionViewFlowLayout *layout;

@end

// 配图视图格子间距
static CGFloat StatusCellPictureItemMargin = 8;

// 可重用id
static NSString *StatusPictureCellId = @"StatusPictureCellId";

@implementation StatusCellPictureView

/**
 *  便利构造函数
 *
 *  @return 
 */
- (instancetype)init {
    
    self.layout.minimumInteritemSpacing = StatusCellPictureItemMargin;
    self.layout.minimumLineSpacing = StatusCellPictureItemMargin;
    
    self = [super initWithFrame:CGRectZero collectionViewLayout:self.layout];
    
    self.backgroundColor = [UIColor orangeColor];
    
    
    // 设置数据源
    self.dataSource = self;
    
    // 注册 cell
    [self registerClass:[StatusCellPictureViewCell class] forCellWithReuseIdentifier:StatusPictureCellId];
    
    return self;
}

// 计算视图大小
- (CGSize)calcViewSize {
    
     // 默认每行 3 张图片
    CGFloat rowCount = 3;
    
    // collectionView 总宽度
    CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width - 2 * StatusCellMagrin;
    
    // 每张图片宽度
    CGFloat itemWidth = (maxWidth - 2 * StatusCellPictureItemMargin) / rowCount;
    
    // 设置itemSize
    self.layout.itemSize = CGSizeMake(itemWidth, itemWidth);
    
    // 图片数量
    NSInteger count = self.viewModle.thumbnailUrls.count;
    
    // 1. 无图
    if (count == 0) {
        return CGSizeZero;
    }
    
    // 2. 单图
    if (count == 1) {
        // TODO: - 临时设置单图大小
        CGSize size = CGSizeMake(150, 120);
        
        // 利用 SDWebImage 检查本地的缓存图像 - key 就是 url 的完整字符串
        NSString *key = self.viewModle.thumbnailUrls.firstObject.absoluteString;
        if (key) {
            UIImage *image = [[[SDWebImageManager sharedManager] imageCache] imageFromDiskCacheForKey:key];
            size = image.size;
        }
        
        // 针对过窄图片处理
        size.width = size.width < 40 ? 40 : size.width;
        
        // 过宽图片处理
        if (size.width > 300) {
            CGFloat w = 300;
            CGFloat h = size.height * w / size.width;
            size = CGSizeMake(w, h);
        }
        
        // 内部图片大小
        self.layout.itemSize = size;
        
        // 配图大小
        return size;
    }
    
    // 3. 四张图，返回 2 * 2
    if (count == 4) {
        CGFloat w = 2 * itemWidth + StatusCellPictureItemMargin;
        return CGSizeMake(w, w);
    }
    
    // 其他数量
    // 行数
    // 4> 其他图片 按照九宫格来显示
    // 计算出行数
    /**
     2 3
     5 6
     7 8 9
     */
    else {
        NSInteger row = (NSInteger)((count - 1) / (int)(rowCount)) + 1;
        CGFloat w = rowCount * itemWidth + (rowCount - 1) * StatusCellPictureItemMargin;
        CGFloat h = row * itemWidth + (row - 1) * StatusCellPictureItemMargin;
        return CGSizeMake(w, h);
    }
}

- (CGSize)sizeThatFits:(CGSize)size {
    
    return [self calcViewSize];
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//    NSLog(@"self.viewModle.thumbnailUrls.count:%zd",self.viewModle.thumbnailUrls.count);
    return self.viewModle.thumbnailUrls.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    StatusCellPictureViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:StatusPictureCellId forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor redColor];
    cell.imageURL = self.viewModle.thumbnailUrls[indexPath.item];
    return cell;
}

#pragma mark - set
- (void)setViewModle:(StatusViewModel *)viewModle {
    if (viewModle) {
        _viewModle = viewModle;
        
        // 会调用 sizeThatFits
        [self sizeToFit];
        
        // 刷新数据 － 如果不刷新，后续的 collectionView 一旦被复用，不再调用数据源方法
        [self reloadData];
    }
}

#pragma mark - 懒加载
-(UICollectionViewFlowLayout *)layout {
    if (!_layout) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
    }
    
    return _layout;
}

@end
